#include <iostream>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
  //此法可以通过，但是速度比较慢，用双指针原地交换法再试
  string reverseWords(string s) {
    stack<char> temp;
    string res;
    s.push_back(' ');
    for (auto p = s.begin(); p != s.end(); ++p) {
      if (*p == ' ') {
        while (!temp.empty()) {
          res.push_back(temp.top());
	  temp.pop();
        }
	res.push_back(' ');
      }else{
	temp.push(*p);
      }
    }
    res.pop_back();
    return res;
  }
};

int main() {
  Solution so;
  string s = "Let's take LeetCode contest";
  cout<<so.reverseWords(s).data()<<endl;
}
